﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace drcoderz.com.clocks.chrono
{
    public class ChronoFrame : Control
    {
        public ChronoFrame()
        {
            this.DefaultStyleKey = typeof(ChronoFrame);
        }

        public event EventHandler OnChronoRightTopClickEvent;
        public event EventHandler OnChronoRightBottomClickEvent;
        public event EventHandler OnChronoLeftTopClickEvent;
        public event EventHandler OnChronoLeftBottomClickEvent;

        /// <summary>
        /// Buttons
        /// </summary>
        private ChronoButton chronoBtnRightTop;
        private ChronoButton chronoBtnRightBottom;
        private ChronoButton chronoBtnLeftTop;
        private ChronoButton chronoBtnLeftBottom;

        private ChronometerWatch chronoWatch;

        public override void OnApplyTemplate()
        {
            chronoWatch = this.GetTemplateChild("chronoWatch") as ChronometerWatch;

            chronoBtnRightTop = this.GetTemplateChild("chronoBtnRightTop") as ChronoButton;
            chronoBtnRightBottom = this.GetTemplateChild("chronoBtnRightBottom") as ChronoButton;
            chronoBtnLeftTop = this.GetTemplateChild("chronoBtnLeftTop") as ChronoButton;
            chronoBtnLeftBottom = this.GetTemplateChild("chronoBtnLeftBottom") as ChronoButton;

            // wire up the events
            chronoBtnRightTop.OnChronoClickEvent += ChronoButtonClick;
            chronoBtnRightBottom.OnChronoClickEvent += ChronoButtonClick;
            chronoBtnLeftTop.OnChronoClickEvent += ChronoButtonClick;
            chronoBtnLeftBottom.OnChronoClickEvent += ChronoButtonClick;

            // apply some defaults
            chronoWatch.HandColor = ChronometerHandColor;
            chronoWatch.Hours = Hours;
            chronoWatch.Minutes = Minutes;
            chronoWatch.Seconds = Seconds;

            //chronoWatch.MiniClockHourValue = MiniClockHourValue;
            //chronoWatch.MiniClockMinuteValue = MiniClockMinuteValue;
            //chronoWatch.MiniClockSecondValue = MiniClockSecondValue;
            
            base.OnApplyTemplate();
        }

        private void ChronoButtonClick(object sender, EventArgs e)
        {
            var btnTag = (sender as ChronoButton).Tag.ToString();

            switch (btnTag)
            {
                case "rightTop":
                    if (OnChronoRightTopClickEvent != null)
                    {
                        OnChronoRightTopClickEvent(this, new EventArgs());
                    }
                    break;
                case "rightBottom":
                    if (OnChronoRightBottomClickEvent != null)
                    {
                        OnChronoRightBottomClickEvent(this, new EventArgs());
                    }
                    break;
                case "leftTop":
                    if (OnChronoLeftTopClickEvent != null)
                    {
                        OnChronoLeftTopClickEvent(this, new EventArgs());
                    }
                    break;
                case "leftBottom":
                    if (OnChronoLeftBottomClickEvent != null)
                    {
                        OnChronoLeftBottomClickEvent(this, new EventArgs());
                    }
                    break;
            }
        }

        public Brush ChronometerHandColor
        {
            get { return (Brush)GetValue(ChronometerHandColorProperty); }
            set {
 
                SetValue(ChronometerHandColorProperty, value);
                if (chronoWatch != null)
                {
                    chronoWatch.HandColor = value;
                }
            }
        }

        // Using a DependencyProperty as the backing store for ChronometerHandColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ChronometerHandColorProperty =
            DependencyProperty.Register("ChronometerHandColor", typeof(Brush), typeof(ChronoFrame), new PropertyMetadata(new SolidColorBrush(Colors.Red)));

        // HANDS

        public int Hours
        {
            get { return (int)GetValue(HoursProperty); }
            set { 
                SetValue(HoursProperty, value);
                if (chronoWatch != null)
                    chronoWatch.Hours = value;
            }
        }

        // Using a DependencyProperty as the backing store for Hours.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HoursProperty =
            DependencyProperty.Register("Hours", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        public int Minutes
        {
            get { return (int)GetValue(MinutesProperty); }
            set { 
                SetValue(MinutesProperty, value);
                if (chronoWatch != null)
                    chronoWatch.Minutes = value;
            }
        }

        // Using a DependencyProperty as the backing store for Minutes.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MinutesProperty =
            DependencyProperty.Register("Minutes", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        public int Seconds
        {
            get { return (int)GetValue(SecondsProperty); }
            set { 
                SetValue(SecondsProperty, value);
                if (chronoWatch != null)
                    chronoWatch.Seconds = value;
            }
        }

        // Using a DependencyProperty as the backing store for Seconds.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SecondsProperty =
            DependencyProperty.Register("Seconds", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        #region MiniClocks Values


        public int MiniClockHourValue
        {
            get { return (int)GetValue(MiniClockHourValueProperty); }
            set { SetValue(MiniClockHourValueProperty, value);
                if (chronoWatch != null)
                    chronoWatch.MiniClockHourValue = value;
            }
        }

        // Using a DependencyProperty as the backing store for MiniClockHourValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MiniClockHourValueProperty =
            DependencyProperty.Register("MiniClockHourValue", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        public int MiniClockMinuteValue
        {
            get { return (int)GetValue(MiniClockMinuteValueProperty); }
            set { SetValue(MiniClockMinuteValueProperty, value);
            if (chronoWatch != null)
                chronoWatch.MiniClockMinuteValue = value;
            }
        }

        // Using a DependencyProperty as the backing store for MiniClockMinuteValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MiniClockMinuteValueProperty =
            DependencyProperty.Register("MiniClockMinuteValue", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        public int MiniClockSecondValue
        {
            get { return (int)GetValue(MiniClockSecondValueProperty); }
            set { 
                SetValue(MiniClockSecondValueProperty, value);
                if (chronoWatch != null)
                    chronoWatch.MiniClockSecondValue = value;
            }
        }

        // Using a DependencyProperty as the backing store for MiniClockSecondValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MiniClockSecondValueProperty =
            DependencyProperty.Register("MiniClockSecondValue", typeof(int), typeof(ChronoFrame), new PropertyMetadata(null));


        #endregion
    }
}
